<template>
  <div style="background-color: #FFFFFF; padding: 20px;">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

      <el-form-item label="所属项目" prop="project">
        <el-select
          style="width: 100%;"
          v-model="optionsProjectId"
          multiple
          filterable
          remote
          reserve-keyword
          placeholder="请输入项目名称"
          :remote-method="getProjectInfo"
          :loading="loading">
          <el-option
            v-for="(item,index) in project_info"
            :key="index"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="所属模块" prop="module">
        <el-select
          style="width: 100%;"
          :disabled="!optionsProjectId.length"
          v-model="optionsModuleId"
          multiple
          filterable
          remote
          reserve-keyword
          placeholder="请输入"
          :remote-method="getModuleInfo"
          :loading="loading">
          <el-option
            v-for="(item,index) in module_info"
            :key="index"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="所属页面" prop="module_page">
        <el-select
          style="width: 100%;"
          :disabled="!optionsModuleId.length"
          v-model="optionsModulePageId"
          multiple
          filterable
          remote
          reserve-keyword
          placeholder="请输入"
          :remote-method="getModulePageInfo"
          :loading="loading">
          <el-option
            v-for="(item,index) in module_page_info"
            :key="index"
            :label="item.title"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="标题" prop="title">
        <el-input v-model="ruleForm.title"></el-input>
      </el-form-item>

      <el-form-item label="函数名称" prop="func_name">
        <el-input v-model="ruleForm.func_name"></el-input>
      </el-form-item>

      <el-form-item label="所属标签" prop="label_element">
        <el-select
          style="width: 100%;"
          :disabled="!optionsModulePageId.length"
          v-model="optionsLabelElementId"
          multiple
          filterable
          remote
          reserve-keyword
          placeholder="请输入标签名称"
          :remote-method="getLabelElementInfo"
          :loading="loading">
          <el-option
            v-for="(item,index) in label_element_info"
            :key="index"
            :label="item.title"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="web入参sql" prop="sql_detail_body">
        <el-select
          style="width: 100%;"
          v-model="optionsSqlDetailBodyId"
          multiple
          filterable
          remote
          reserve-keyword
          placeholder="请输入"
          :remote-method="getSqlDetailBodyInfo"
          :loading="loading">
          <el-option
            v-for="(item,index) in sql_detail_body_info"
            :key="index"
            :label="item.sql_title"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="web断言sql" prop="sql_detail_assert">
        <el-select
          style="width: 100%;"
          v-model="optionsSqlDetailAssertId"
          multiple
          filterable
          remote
          reserve-keyword
          placeholder="请输入"
          :remote-method="getSqlDetailAssertInfo"
          :loading="loading">
          <el-option
            v-for="(item,index) in sql_detail_assert_info"
            :key="index"
            :label="item.sql_title"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="备注" prop="remarks">
        <el-input v-model="ruleForm.remarks" type="textarea" :autosize="{ minRows: 2, maxRows: 6}"></el-input>
      </el-form-item>

      <el-form-item label="入参示例" prop="into_example">
        <el-input v-model="ruleForm.into_example" type="textarea" :autosize="{ minRows: 2, maxRows: 6}"></el-input>
      </el-form-item>

      <el-form-item label="出参示例" prop="out_example">
        <el-input v-model="ruleForm.out_example" type="textarea" :autosize="{ minRows: 2, maxRows: 6}"></el-input>
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="ruleForm.status" placeholder="请选择状态" style="width: 100%;">
          <el-option
            v-for="(item,index) in optionsCommon"
            :key="index"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm()">保存</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>
<script>
  export default {
    name: 'EditSeleniumFunc',
    data() {
      return {

        optionsCommon: [],  // 存储CommonChoices实际数据的列表

        EditUrl: this.$http.SeleniumFuncURL,  // get编辑的URL
        EditIdStr: 'selenium_func_id',  // 模块id字符串
        pushObjName: 'SeleniumFunc',  // 成功后跳转的路由

        // 编辑和添加选中的数据id
        loading: false,
        optionsLabelElementId: [],
        optionsProjectId: [],
        optionsModuleId: [],
        optionsModulePageId: [],
        optionsSqlDetailBodyId: [],
        optionsSqlDetailAssertId: [],

        label_element_info: [],
        project_info: [], // 项目列表，获取1000条项目数据
        module_info: [], // 模块列表，获取1000条项目数据
        module_page_info: [], // 页面列表
        sql_detail_body_info: [], // 请求体sql明细
        sql_detail_assert_info: [], // 断言sql明细

        ruleForm: {
          project: null,
          module: null,
          module_page: null,
          title: '',//标题
          func_name: '',//函数名称
          sql_detail_body: '',  // 入参sql
          sql_detail_assert: '',  // 断言sql
          remarks: '', //备注
          label_element: [],
          into_example: null,//示例
          out_example: null,//示例
          status: '',//状态

          project_info: '',
          module_info: '',
          module_page_info: '',
          sql_body_info: {}, // 请求体sql数据
          sql_assert_info: {}, // 断言sql数据
        },  //
        rules: {
          env_title: [
            {required: true, message: '请输入标题', trigger: 'blur'}
          ],
        }
      };
    },
    created() {
      this.getEditData();   // get回将要编辑的数据
      // this.getLabeleLeMent();
      this.optionsCommon = this.getChoices.common
    },
    methods: {
      submitForm() {
        // this.ruleForm.label_element = this.optionsLabelElementId;
        this.ruleForm.label_element = [];
        this.optionsLabelElementId.forEach(lis => {
          this.ruleForm.label_element.push(lis)
        });
        console.log(this.ruleForm);
        this.$myTool.submitForm(this);
        console.log(this.ruleForm);
      },

      // get回将要编辑的数据
      getEditData() {
        this.$myTool.getEditData(this)
      },

      // 项目信息搜索
      getProjectInfo(query) {
        this.$myTool.getObjList(this, this.$http.projectURL, this.project_info, query)
      },

      // 获取模块信息
      getModuleInfo(query) {
        let QuerySet = {
          module_project: this.ruleForm.project,
          inputObjName: query
        };
        this.$myTool.getObjList(this, this.$http.moduleURL, this.module_info, QuerySet)
      },

      // 获取页面信息
      getModulePageInfo(query) {
        let QuerySet = {
          module_page_module_id: this.ruleForm.module,
          module_page_project_id: this.ruleForm.project,
          inputObjName: query
        };
        this.$myTool.getObjList(this, this.$http.ModulePageURL, this.module_page_info, QuerySet)
      },

      // 标签信息搜索
      getLabelElementInfo(query) {
        let QuerySet = {
          module_page_id: this.ruleForm.module_page,
          inputObjName: query
        };
        this.$myTool.getObjList(this, this.$http.LabelElementURL, this.label_element_info, QuerySet)
      },

      // 请求体sql信息
      getSqlDetailBodyInfo(query) {
        this.$myTool.getObjList(this, this.$http.sqlURL, this.sql_detail_body_info, query)
      },
      // 断言sql信息
      getSqlDetailAssertInfo(query) {
        this.$myTool.getObjList(this, this.$http.sqlURL, this.sql_detail_assert_info, query)
      },

      //
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },
    watch: {

      'ruleForm.project_info': function (value) {
        if (value) {
          this.optionsProjectId.push(value.id);
          this.project_info.push(value);
        } else {
          this.optionsProjectId = [];
          this.project_info = [];
        }
      },
      optionsProjectId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
        this.ruleForm.project = value.length ? value[0] : null
      },

      'ruleForm.module_info': function (value) {
        if (value) {
          this.optionsModuleId.push(value.id);
          this.module_info.push(value);
        } else {
          this.optionsModuleId = [];
          this.module_info = [];
        }
      },
      optionsModuleId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
        this.ruleForm.module = value.length ? value[0] : null
      },

      'ruleForm.module_page_info': function (value) {
        if (value) {
          this.optionsModulePageId.push(value.id);
          this.module_page_info.push(value);
        } else {
          this.optionsModulePageId = [];
          this.module_page_info = [];
        }
      },
      optionsModulePageId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
        this.ruleForm.module_page = value.length ? value[0] : null
      },

      'ruleForm.label_element': function (value) {
        this.optionsLabelElementId = [];
        this.label_element_info = [];
        if (value.length) {
          this.optionsLabelElementId = value;
          let QuerySet = {
            label_element_in_id: value
          };
          this.$myTool.getObjList(this, this.$http.LabelElementURL, this.label_element_info, QuerySet)
        }
      },
      'ruleForm.sql_body_info': function (value) {
        if (value) {
          this.optionsSqlDetailBodyId.push(value.id);
          this.sql_detail_body_info.push(value);
        } else {
          this.optionsSqlDetailBodyId = [];
          this.sql_detail_body_info = [];
        }
      },
      optionsSqlDetailBodyId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
        this.ruleForm.sql_detail_body = (value.length === 1) ? value[0] : null;
      },

      'ruleForm.sql_assert_info': function (value) {
        if (value) {
          this.optionsSqlDetailAssertId.push(value.id);
          this.sql_detail_assert_info.push(value);
        } else {
          this.optionsSqlDetailAssertId = [];
          this.sql_detail_assert_info = [];
        }
      },
      optionsSqlDetailAssertId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
        this.ruleForm.sql_detail_assert = (value.length === 1) ? value[0] : null;
      },
    },
    computed: {
      getChoices() {
        return this.$store.state.choicesInfo
      }
    },
  }
</script>
